/*
 * 病例库
 *
 * @param disorderID 
 * @param name 
 */

var Disorder = Class.create( {
    //lizhen 2018-05-21 添加部位参数 bPart
    initialize: function(disorderID, name, callWhenReady,age,ageType,bPart,cancerId) {
        if (name == null && !isInt(disorderID)) {
            name = Disorder.desanitizeID(disorderID);
        }
        this._disorderID = Disorder.sanitizeID(disorderID);
        this._name       = name ? name : "加载...";
        this._age=age?age:"";
        this._ageType=ageType?ageType:"";
        this._bPart = bPart ? bPart : "";
        this._cancerId = cancerId ? cancerId : "";
        if (!name && callWhenReady)
            this.load(callWhenReady);
    },
    getDisorderID: function() {
        return this._disorderID;
    },
 
    getName: function() {
        return this._name;
    },

    getAge: function() {
        return this._age;
    },
    getAgeType:function() {
        return this._ageType;
    },
    setAge:function (age) {
        this._age=age;
    },
    setAgeType:function (ageType) {
        this._ageType=ageType;
    },
    //lizhen 2018-05-21 获取和设置部位参数 bPart
    setBpart:function(bPart){
        this._bPart = bPart;
    },
    getBpart:function(){
        return this._bPart;
    },
    getCancerId:function(){
        return this._cancerId;
    },
    setCancerId:function(cancerId){ 
        this._cancerId = cancerId;
    },
    load: function(callWhenReady) {
        var baseOMIMServiceURL = Disorder.getOMIMServiceURL();
        var queryURL           = baseOMIMServiceURL + "&q=id:" + this._disorderID;
        console.log(baseOMIMServiceURL);
        new Ajax.Request(queryURL, {
            method: "GET",
            onSuccess: this.onDataReady.bind(this),
            onComplete: callWhenReady ? callWhenReady : {}
        });
    },

    onDataReady : function(response) {
        try {
            var parsed = JSON.parse(response.responseText);
            console.log("加载病历库: disorder id = " + this._disorderID + ", name = " + parsed.rows[0].name);
            this._name = parsed.rows[0].name;
        } catch (err) {
            console.log("[加载病历库] Error: " +  err);
        }
    }
});

Disorder.sanitizeID = function(disorderID) {
    if (isInt(disorderID))
        return disorderID;
    //var temp = disorderID.replace(/[\(\[]/g, '_L_');
    //temp = temp.replace(/[\)\]]/g, '_J_');
    return disorderID;
}

Disorder.desanitizeID = function(disorderID) {
    //var temp = disorderID.replace(/__/g, " ");
    //temp = temp.replace(/_L_/g, "(");
    return disorderID;
}

Disorder.getOMIMServiceURL = function() {
    return 'http://'+location.host + (new XWiki.Document('OmimService', 'PhenoTips').getURL("get", "outputSyntax=plain"));
}
